//=============================================================================
// System  : MatchKit
// File    : SpacesPattern.cs
// Author  : Fabio Bussu  (matchkit@fabiobussu.it)
// Updated : 04/16/2013
// Note    : Copyright 2013, Fabio Bussu, All rights reserved
//
// This code is published under the Microsoft Public License (Ms-PL). A copy
// of the license should be distributed with the code.  It can also be found
// at the project website: http://matchkit.codeplex.com. This notice, the
// author's name, and all copyright notices must remain intact in all
// applications, documentation, and source files.
//
// Version     Date     Who  Comments
// ============================================================================
// 1.0.0.0  04/16/2013   FB  First draft
//=============================================================================
using System;
using System.Runtime.Serialization;

namespace MatchKit.Patterns
{
	/// <summary>
	/// Provides the base class with the logic to match zero or more spaces, including or not newlines.
	/// </summary>
	/// <threadsafety static="true" instance="false" />
	/// <serialization binary="true" contract="true" />
	/// <conceptualLink target="0a06b2a9-5f58-409a-ba62-639cfd77bca7" />
	#if DC
	[DataContract]
	#endif
	#if SZ
	[Serializable]
	#endif
	public abstract class SpacesPattern : BasePattern
	{
		/// <summary>
		/// Creates a pattern to match one or more spaces.
		/// </summary>
		/// <param name="includeNewLines">Value specifing whether including newline in the match.</param>
		/// <returns>The created pattern.</returns>
		public static IPattern OneOrMore(bool includeNewLines = false)
		{
			return new OneOrMoreSpacesPattern(includeNewLines);
		}

		/// <summary>
		/// Creates a pattern to match zero or more spaces.
		/// </summary>
		/// <param name="includeNewLines">Value specifing whether including newline in the match.</param>
		/// <returns>The created pattern.</returns>
		public static IPattern ZeroOrMore(bool includeNewLines = false)
		{
			return new OneOrMoreSpacesPattern(includeNewLines);
		}

		/// <summary>
		/// Creates a pattern to match one space.
		/// </summary>
		/// <param name="includeNewLines">Value specifing whether including newline in the match.</param>
		/// <returns>The created pattern.</returns>
		public static IPattern Single(bool includeNewLines = false)
		{
			return new WhiteSpacePattern(includeNewLines);
		}

		/// <summary>
		/// Creates a pattern to match one new line (CR, LF or CRLF).
		/// </summary>
		/// <returns>The created pattern.</returns>
		public static IPattern NewLine()
		{
			return new NewLinePattern();
		}


		/// <summary>
		/// Initializes a new instance of the <see cref="T:MatchKit.Patterns.SpacesPattern"/> class. 
		/// </summary>
		protected SpacesPattern()
		{
			// empty
		}

		private bool _includeNewLines;
		/// <summary>
		/// Gets or sets a value specifing whether including newlines in the match.
		/// <br/>
		/// Default vaule is <b>false</b>.
		/// </summary>
		/// <serialization member="true" />
		#if DC
		[DataMember]
		#endif
		public bool IncludeNewLines
		{
			get { return _includeNewLines; }
			set { _includeNewLines = value; }
		}

		private bool _permitsZeroLength;
		/// <summary>
		/// Gets or sets a value specifing whether the match will success also with zero spaces.
		/// <br/>
		/// Default vaule is <b>false</b>.
		/// </summary>
		/// <serialization member="true" />
		#if DC
		[DataMember]
		#endif
		public bool PermitsZeroLength
		{
			get { return _permitsZeroLength; }
			set { _permitsZeroLength = value; }
		}

		/// <inheritdoc />
		protected override Match OnMatch(MatchContext context)
		{
			if (!IsSpace(context.Current))
			{
				if (_permitsZeroLength)
					return Optional(context, null);
				else
					return Error(context);
			}

			context.NextTo(ch => IsSpace(ch));
			return Success(context, MatchedString.Value);
		}

		/// <inheritdoc />
		public override string HelpString
		{
			get
			{
				if (_permitsZeroLength)
				{
					if (_includeNewLines)
						return "Zero or more spaces included newlines";
					else
						return "Zero or more spaces excluded newlines";
				}
				else
				{
					if (_includeNewLines)
						return "One or more spaces included newlines";
					else
						return "One or more spaces excluded newlines";
				}
			}
		}

		/// <summary>
		/// Returns true whether the parameter is a space.
		/// </summary>
		/// <returns>The result of the check.</returns>
		protected bool IsSpace(char ch)
		{
			var iws = char.IsWhiteSpace(ch);
			if (!iws)
				return false;
			if (_includeNewLines)
				return true;
			// il new-line e' identificato o dallo \r o dallo \n, comunque lo \r viene prima
			return ch != '\r' && ch != '\n';
		}
	}
}
